#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
vector<int> arr;
bool vis[1000008];
int tmp = 0;
int n, ret;
void dfs(int x, int j)
{
	tmp++;
	if (!vis[x])
	{
		vis[x] = true;
		dfs(arr[x], j);
	}
	


	if (arr[x] - 1 >= 0 && arr[x] == j)
		dfs(arr[x] - 1, j);
	if (arr[x] + 1 < n && arr[x] == j)
		dfs(arr[x] + 1, j);

}
/*
5
2 1 5 4 3
*/
int main()
{
	cin >> n;
	arr.resize(n + 1);
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			memset(vis, false, sizeof vis);
			tmp = 0;
			dfs(i, j);
			ret = max(ret, tmp);
		}
	}
	cout << ret;
	return 0;
}
